﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace HomeStayWebCP.Utility
{
    public class CryptorEngine 
    {
        public string Encrypt(string ToEncrypt, bool useHasing)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(ToEncrypt);
                //System.Configuration.AppSettingsReader settingsReader = new     AppSettingsReader();
                string Key = "khoanangochtnhoangdxchug";//"Bhagwatitkhoakhoakhoakho";
                if (useHasing)
                {
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
                    hashmd5.Clear();
                }
                else
                {
                    keyArray = UTF8Encoding.UTF8.GetBytes(Key);
                }
                TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
                tDes.Key = keyArray;
                tDes.Mode = CipherMode.ECB;
                tDes.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = tDes.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                tDes.Clear();
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public string Decrypt(string cypherString, bool useHasing)
        {
            try
            {
                byte[] keyArray;
                byte[] toDecryptArray = Convert.FromBase64String(cypherString);
                //byte[] toEncryptArray = Convert.FromBase64String(cypherString);
                //System.Configuration.AppSettingsReader settingReader = new     AppSettingsReader();
                string key = "khoanangochtnhoangdxchug";
                if (useHasing)
                {
                    MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
                    keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                    hashmd.Clear();
                }
                else
                {
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);
                }
                TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
                tDes.Key = keyArray;
                tDes.Mode = CipherMode.ECB;
                tDes.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = tDes.CreateDecryptor();

                try
                {
                    byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);

                    tDes.Clear();
                    return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch
            {
                return null;
            }
        }

    }
}
